#include "actionmanager.h"
#include "character.h"
#include "state.h"




void CActionManager::setOwner(ICharacter* owner)
{
	_owner = owner;
}

void CActionManager::Insert(CActionBase* action)
{
	while(!_actionlist.empty())
	{
		CActionBase* preAction = _actionlist.front();
		preAction->Exit(_owner);
		_actionlist.pop_front();
	}

	action->Enter(_owner);
	_actionlist.push_back(action);
}

ACTION::TYPE CActionManager::GetCurrActionType()
{
	if(_actionlist.empty())
	{
		assert(0);
		return ACTION::TYPE::STD;
	}

	CActionBase* curraction = _actionlist.front();
	return curraction->GetType();
}

void CActionManager::Update()
{
	if(!_actionlist.empty())
	{
		CActionBase* action = _actionlist.front();

		if(_owner->_lifetime != INFINITE && 
			_owner->_elapseTime.GetMilliseconds() > _owner->_lifetime)
		{
			action->Exit(_owner);
			_actionlist.pop_front();
		}
		else
		{
			action->Process(_owner);
		}
	}

	if(_actionlist.empty())
	{
		_owner->_fsmManager.ChangeState(CStandState::GetInstance());
		return;
	}
}